package pers.vic.practice.util;

import org.springframework.web.util.WebUtils;
import pers.vic.boot.base.enums.CommonMsgEnum;
import pers.vic.boot.base.exception.CommonException;
import pers.vic.boot.base.tool.Tools;
import pers.vic.practice.base.constant.BaseConstant;
import pers.vic.practice.base.enums.UserType;
import pers.vic.practice.base.model.Principal;

import javax.servlet.http.HttpServletRequest;

/**
 * 描述: session
 * @author Vic.xu
 * @date 2022-04-12 16:52
 */
public class SystemSessionHelper {

    /***
     * 当前用户
     */
    public static Principal currentUser(){
        HttpServletRequest request = Tools.currentRequest();
        return  (Principal) WebUtils.getSessionAttribute(request, BaseConstant.USER_SESSION_KEY);
    }

    /**
     * 当前登录用户的学号
     * @return
     */
    public static String getNumber(){
        Principal principal = currentUser();
        if (principal == null) {
            throw  new CommonException(CommonMsgEnum.NOT_LOGIN);
        }
        if (principal.getUserType() != UserType.student) {
            throw  new CommonException(500, "当前用户非学生");
        }
        return principal.getStudent().getNumber();
    }

}
